package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.ExecuteQuery;


public class SearchItem extends JPanel {

	private CardLayout basePanel;
	private CardLayout c1;
	private JFrame thisFrame;
	private JComboBox catBox;
	private JTextField keywordField;
	private JTextField minPriceField;
	private JTextField maxPriceField;
	private JComboBox conBox;

	/**
	 * Create the panel.
	 */
	public SearchItem(final CardLayout c1, final JPanel basePanel, final JFrame thisFrame) {
		this.c1 = c1;
		this.basePanel = c1;
		this.thisFrame = thisFrame;
		thisFrame.setTitle("Item Search");
		thisFrame.setBounds(MainWindow.XCENTER - 200, MainWindow.YCENTER - 175, 400, 350);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		setBackground(Color.WHITE);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.WEST;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.VERTICAL;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		
		JLabel lblKeyword = new JLabel("Keyword");
		panel.add(lblKeyword);
		panel.setBackground(Color.WHITE);
		
		keywordField = new JTextField();
		panel.add(keywordField);
		keywordField.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.WEST;
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 1;
		add(panel_1, gbc_panel_1);
		
		JLabel lblCategory = new JLabel("Category");
		panel_1.add(lblCategory);
		
		catBox = new JComboBox(NewItem.catValues);
		panel_1.add(catBox);
		panel_1.setBackground(Color.WHITE);
		
		JPanel panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.anchor = GridBagConstraints.WEST;
		gbc_panel_2.insets = new Insets(0, 0, 5, 0);
		gbc_panel_2.fill = GridBagConstraints.VERTICAL;
		gbc_panel_2.gridx = 1;
		gbc_panel_2.gridy = 2;
		add(panel_2, gbc_panel_2);
		
		JLabel lblMinimumPrice = new JLabel("Minimum Price $");
		panel_2.add(lblMinimumPrice);
		panel_2.setBackground(Color.WHITE);
		
		minPriceField = new JTextField();
		panel_2.add(minPriceField);
		minPriceField.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.anchor = GridBagConstraints.WEST;
		gbc_panel_3.insets = new Insets(0, 0, 5, 0);
		gbc_panel_3.fill = GridBagConstraints.VERTICAL;
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 3;
		add(panel_3, gbc_panel_3);
		
		JLabel lblMaximumPrice = new JLabel("Maximum Price $");
		panel_3.add(lblMaximumPrice);
		
		maxPriceField = new JTextField();
		panel_3.add(maxPriceField);
		panel_3.setBackground(Color.WHITE);
		maxPriceField.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.anchor = GridBagConstraints.WEST;
		gbc_panel_4.insets = new Insets(0, 0, 5, 0);
		gbc_panel_4.fill = GridBagConstraints.VERTICAL;
		gbc_panel_4.gridx = 1;
		gbc_panel_4.gridy = 4;
		add(panel_4, gbc_panel_4);
		
		JLabel lblConditionAtLeast = new JLabel("Condition at least");
		panel_4.add(lblConditionAtLeast);
		
		conBox = new JComboBox(NewItem.conValues);
		panel_4.add(conBox);
		
		JPanel panel_5 = new JPanel();
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.anchor = GridBagConstraints.EAST;
		gbc_panel_5.fill = GridBagConstraints.VERTICAL;
		gbc_panel_5.gridx = 1;
		gbc_panel_5.gridy = 5;
		add(panel_5, gbc_panel_5);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				thisFrame.setBounds(MainWindow.XCENTER - 275, MainWindow.YCENTER - 300, 550, 600);
				c1.first(basePanel);
			}
		});
		panel_5.add(btnCancel);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				String keyword = keywordField.getText();
				String category = NewItem.catValues[catBox.getSelectedIndex()];
				double minimumPrice = Double.parseDouble(minPriceField.getText());
				double maximumPrice = Double.parseDouble(maxPriceField.getText());
				String conditionRAW = NewItem.conValues[conBox.getSelectedIndex()];
				
				int condition = 0;
				if (conditionRAW.equals("Fair")) condition = 1;
				if (conditionRAW.equals("Good")) condition = 2;
				if (conditionRAW.equals("Very Good")) condition = 3;
				if (conditionRAW.equals("New")) condition = 4;
				
				ExecuteQuery.searchItem(keyword, category, minimumPrice, maximumPrice, condition);
				
				SearchResults search = new SearchResults(c1, basePanel, thisFrame);
				basePanel.add(search, "SEARCHPANEL");
				c1.show(basePanel, "SEARCHPANEL");
			}
		});
		panel_5.add(btnSearch);
		

	}

}
